﻿using System;
using System.Diagnostics;
using Soundteller.Audio.Services.Interfaces;

namespace Soundteller.Audio.Services
{
    public class BeatCalculator : IBeatCalculator
    {
        public double Calculate(TimeSpan time, double bpm)
        {
            Debug.WriteLine("{0}.Calculate( {1}, {2} )", GetType().Namespace, time, bpm);

            if (bpm == 0) return 0;

            var milisecondsPerBeat = CalculateMilliSecondsPerBeat(bpm);

            return (time.TotalMilliseconds / milisecondsPerBeat);
        }

        private double CalculateMilliSecondsPerBeat(double bpm)
        {
            var beatsPerMillisecond = (bpm / 60000);
            var milisecondsPerBeat = (1 / beatsPerMillisecond);

            return milisecondsPerBeat;
        }
    }
}
